/**
 * 作业过程管理
 */
Ext.define('Module.jc.BzzyZygc', {
	extend : 'Ext.panel.Panel',
	alias : 'widget.bzzyMajor',
	layout: 'border',
	config:{
		zytype:''
	},
	initComponent : function() {
		var me = this;
		
		/**
		 * 作业类型store
		 */
		var zytypeStore=Ext.create('Ext.data.Store',{
			autoLoad: true,
			fields: ['id','name','code'],
	        proxy: {
	            type: 'ajax',
	            url: 'bzzy/BzzyZygcAction/getJcZyType.do'
	        }
		});
		
		/**
		 * 作业过程
		 */
		var store=Ext.create('Ext.data.Store',{
			autoLoad: false,
			fields: ['id','zytypeId','zygcName','upuser','uptime','name','describe','sort'],
	        proxy: {
	            type: 'ajax',
	            url: 'bzzy/BzzyZygcAction/getBzzyZygc.do'
	        }
		});
		
		
		/**
		 *  作业类型信息
		 */
		var zytypePanel = me.zytypePanel = Ext.create('Ext.grid.Panel',{
			title: '作业类型',
			region: 'west',
			padding: '0 1 0 0',
			width:280,
			store: zytypeStore,
			tbar:[{
				xtype: 'panel',
				border: false,
				height: 25,
				width: 280,
				bodyPadding: 0-1,
				layout: 'column',
				defaults: {columnWidth: 1,bodyPadding: 0},
				items: [{
						xtype: 'toolbar',
						items: [{
							text: '添加信息',
							iconCls: 'icon-add',
							handler: function(){
									me.addZyType();
							}
						},{
							text: '修改信息',
							iconCls: 'icon-edit',
							handler: function(){
								var rec = zytypePanel.getSelectionModel().getSelection()[0];
								if(rec){
									me.updateJcZyType(rec);
								}
								else{
									Ext.Msg.alert('提示', '请选择要修改的信息！');
								}
							}
						},{
							text: '删除信息',
							iconCls: 'icon-delete',
							handler: function(){
								var rec = zytypePanel.getSelectionModel().getSelection()[0];
								if(rec){
									me.deleteJcZyType(rec);
								}
								else{
									Ext.Msg.alert('提示', '请选择要删除的信息！');
								}
							}
						}]
				 }]
			}],
			columns: [{
	            flex: 2,
	            text: '类型名称',
	            sortable: false,
	            dataIndex: 'name'
			}],
			listeners:{
				itemclick:function(tree, record, item, index, e, eOpts) { 
					me.setZytype(record.data.id);
					store.proxy.extraParams ={
							zytypeId:record.data.id
					};
					store.load();
				}
			}
			
		});
		
		
		var bzzyZygcPanel = me.bzzyZygcPanel = Ext.create('Ext.grid.Panel',{
			title: '作业过程信息列表',
			region: 'center',
			padding: '0 1 0 0',
			layout: 'column',
			store: store,
			tbar:[{
				xtype: 'panel',
				border: false,
				height: 25,
				width: '100%',
				bodyPadding: 0-1,
				layout: 'column',
				defaults: {columnWidth: 1,bodyPadding: 0},
				items: [
				     {
						xtype: 'toolbar',
						items: [{
							text: '添加信息',
							iconCls: 'icon-add',
							handler: function(){
								var rec = zytypePanel.getSelectionModel().getSelection()[0];
								if(rec){
									me.addBzzyZygc(rec);
									Ext.getCmp(me.id+"zytypeId").setValue(rec.data.id);
									Ext.getCmp(me.id+"zytypeName").setValue(rec.data.name);
								}
								else{
									Ext.Msg.alert('提示', '请选择要添加的作业类型！');
								}
							}
						},{
							text: '修改信息',
							iconCls: 'icon-edit',
							handler: function(){
								var rec = bzzyZygcPanel.getSelectionModel().getSelection()[0];
								if(rec){
									me.updateBzzyZygc(rec);
								}
								else{
									Ext.Msg.alert('提示', '请选择要修改的信息！');
								}
							}
						},{
							text: '删除信息',
							iconCls: 'icon-delete',
							handler: function(){
								var rec = bzzyZygcPanel.getSelectionModel().getSelection()[0];
								if(rec){
									me.delBzzyZygc(rec);
								}
								else{
									Ext.Msg.alert('提示', '请选择要删除的信息！');
								}
							}
						},{
							xtype:"textfield",
							fieldLabel : '过程名称',	
							maxLength:40,
							labelWidth:60,
							width:195
						},{
							text:"查询",
							iconCls: 'icon-search',
							handler: function(){
								var tbar = this.up('toolbar');
								var items = tbar.items.items;
								var rec = zytypePanel.getSelectionModel().getSelection()[0]
								if(rec){
									bzzyZygcPanel.store.proxy.extraParams = {
										zygcName : items[3].getSubmitValue(),
										zytypeId : rec.data.id//me.getZytype()
									};
									bzzyZygcPanel.store.load();
								}
							}
						}
					]
					 }]
			}],
			columns: [{
	            text: '工作顺序',
	            sortable: false,
	            dataIndex: 'sort',
	            flex: 1
			},{
	            text: '过程名称',
	            sortable: false,
	            flex: 2,
	            dataIndex: 'zygcName'
			},{
	            text: '描述',
	            sortable: false,
	            flex: 4,
	            dataIndex: 'describe'
			},{
	            text: '更新用户',
	            sortable: false,
	            flex: 2,
	            dataIndex: 'name'
			},{
	            text: '更新时间',
	            sortable: false,
	            flex: 2,
	            dataIndex: 'uptime',
	            renderer: formatDate
			}], 
			viewConfig: {  
	            plugins: {  
	                ptype: "gridviewdragdrop",  
	                ddGroup:'DragDropGroup',
	                dragText: "可用鼠标拖拽进行上下排序"  
	            },  
	            listeners: {
		            drop: function(node, data, dropRec, dropPosition) {
		                //拖拽的时候刷新列表即可
		            	me.paixu();
	                 	
	                }
	            }  
	        }
	      
		});
		
		
		me.items = [zytypePanel,bzzyZygcPanel];
		
		me.callParent();
	},
	updateBzzyZygc: function(rec){
		var me = this;
		Ext.create('Ext.window.Window',{
			width: 410,
			height: 220,
			iconCls: 'icon-edit',
			modal: true,
			layout: 'fit',
			title: '修改信息',
			buttons: [{
				text: '保存',
				handler: function(){
					var formp = this.up('panel').items.items[0];
        	    	var form = formp.getForm();
        	    	if(form.isValid()){
        	    		form.submit({
            	    	    url: 'bzzy/BzzyZygcAction/updateBzzyZygc.do',
            	    	    success: function(form, action) {
            	    	    	var rs = Ext.decode(action.response.responseText);
            	    	    	if(rs.rst){
            	    	    		Ext.Msg.alert('提示', '修改成功！');
            	    	    		formp.up('window').close();
            	    	    		
            	    	    		me.bzzyZygcPanel.store.reload();
            	    	    	}
            	    	    	else{
            	    	    		Ext.Msg.alert('提示', '修改失败！详情：'+rs.msg);
            	    	    	}
            	    	    }
            	    	});
        	    	}
        	    	else{
        	    		Ext.Msg.alert('提示', '无效的信息,请正确填写信息！');
        	    	}
				}
			},{
				text: '取消',
				handler: function(){
					this.up('window').close();
				}
			}],
			items: me.getForm({
				bodyPadding: 20,
				border: false,
				listeners: {
					afterrender: function(fp){
						if(rec){
							var type = me.zytypePanel.getSelectionModel().getSelection()[0];
							rec.data.name = type.data.name;
						}
						fp.loadRecord(rec||{});
					}
				}
			})
		}).show();
	},
	addBzzyZygc: function(rec){
		var me = this;
		Ext.create('Ext.window.Window',{
			width: 410,
			height: 220,
			iconCls: 'icon-add',
			modal: true,
			layout: 'fit',
			title: '添加信息',
			buttons: [{
				text: '保存',
				handler: function(){
					var formp = this.up('panel').items.items[0];
        	    	var form = formp.getForm();
        	    	if(form.isValid()){
        	    		form.submit({
            	    	    url: 'bzzy/BzzyZygcAction/addBzzyZygc.do',
            	    	    success: function(form, action) {
            	    	    	var rs = Ext.decode(action.response.responseText);
            	    	    	if(rs.rst){
            	    	    		Ext.Msg.alert('提示', '保存成功！');
            	    	    		formp.up('window').close();
            	    	    		me.bzzyZygcPanel.store.load({
            	    	    			params:{
            	    	    				zytypeId:rec.data.id
            	    	    			}
            	    	    		});
            	    	    	}
            	    	    	else{
            	    	    		Ext.Msg.alert('提示', '保存失败！详情：'+rs.msg);
            	    	    	}
            	    	    }
            	    	});
        	    	}
        	    	else{
        	    		Ext.Msg.alert('提示', '无效的信息,请正确填写信息！');
        	    	}
				}
			},{
				text: '取消',
				handler: function(){
					this.up('window').close();
				}
			}],
			items: me.getForm({
				bodyPadding: 20,
				border: false,
				listeners: {
					afterrender: function(fp){
						fp.loadRecord(rec||{});
					}
				}
			})
		}).show();
	},
	updateJcZyType: function(rec){
		var me = this;
		Ext.create('Ext.window.Window',{
			width: 410,
			height: 160,
			iconCls: 'icon-edit',
			modal: true,
			layout: 'fit',
			title: '修改信息',
			buttons: [{
				text: '保存',
				handler: function(){
					var formp = this.up('panel').items.items[0];
        	    	var form = formp.getForm();
        	    	if(form.isValid()){
        	    		form.submit({
            	    	    url: 'bzzy/BzzyZygcAction/updateJcZyType.do',
            	    	    success: function(form, action) {
            	    	    	var rs = Ext.decode(action.response.responseText);
            	    	    	if(rs.rst){
            	    	    		Ext.Msg.alert('提示', '修改成功！');
            	    	    		formp.up('window').close();
            	    	    		me.zytypePanel.store.reload();
            	    	    	}
            	    	    	else{
            	    	    		Ext.Msg.alert('提示', '修改失败！详情：'+rs.msg);
            	    	    	}
            	    	    }
            	    	});
        	    	}
        	    	else{
        	    		Ext.Msg.alert('提示', '无效的信息,请正确填写信息！');
        	    	}
				}
			},{
				text: '取消',
				handler: function(){
					this.up('window').close();
				}
			}],
			items: me.getZyTypeForm({
				bodyPadding: 20,
				border: false,
				listeners: {
					afterrender: function(fp){
						fp.loadRecord(rec||{});
					}
				}
			})
		}).show();
	},
	delBzzyZygc: function(rec){
		var me = this;
		if(rec){
			Ext.Msg.confirm('提示', '你确定要删除当前信息吗？',function(b){
				if('yes' == b){
					Ext.Ajax.request({
					    url: 'bzzy/BzzyZygcAction/deleteBzzyZygc.do',
					    params: rec.data,
					    success: function(response){
        	    	    	var rs = Ext.decode(response.responseText);
        	    	    	if(rs.rst){
        	    	    		Ext.Msg.alert('提示', '删除成功！');
        	    	    		me.paixu(rec.data.id);
        	    	    	}
        	    	    	else{
        	    	    		Ext.Msg.alert('提示', '删除失败！详情：'+rs.msg);
        	    	    	}
					    }
					});
				}
			});
		}
		else{
			Ext.Msg.alert('提示', '请选择要删除的信息！');
		}
	},
	addZyType: function(rec){
		var me = this;
		Ext.create('Ext.window.Window',{
			width: 410,
			height: 160,
			iconCls: 'icon-add',
			modal: true,
			layout: 'fit',
			title: '添加信息',
			buttons: [{
				text: '保存',
				handler: function(){
					var formp = this.up('panel').items.items[0];
        	    	var form = formp.getForm();
        	    	if(form.isValid()){
        	    		form.submit({
            	    	    url: 'bzzy/BzzyZygcAction/addJcZyType.do',
            	    	    success: function(form, action) {
            	    	    	var rs = Ext.decode(action.response.responseText);
            	    	    	if(rs.rst){
            	    	    		Ext.Msg.alert('提示', '保存成功！');
            	    	    		formp.up('window').close();
            	    	    		me.zytypePanel.store.load();
            	    	    	}
            	    	    	else{
            	    	    		Ext.Msg.alert('提示', '保存失败！详情：'+rs.msg);
            	    	    	}
            	    	    }
            	    	});
        	    	}
        	    	else{
        	    		Ext.Msg.alert('提示', '无效的信息,请正确填写信息！');
        	    	}
				}
			},{
				text: '取消',
				handler: function(){
					this.up('window').close();
				}
			}],
			items: me.getZyTypeForm({
				bodyPadding: 20,
				border: false,
				listeners: {
					afterrender: function(fp){
						fp.loadRecord(rec||{});
					}
				}
			})
		}).show();
	},
	deleteJcZyType: function(rec){
		var me = this;
		if(rec){
			Ext.Msg.confirm('提示', '你确定要删除当前信息吗？',function(b){
				if('yes' == b){
					Ext.Ajax.request({
					    url: 'bzzy/BzzyZygcAction/deleteJcZyType.do',
					    params: rec.data,
					    success: function(response){
        	    	    	var rs = Ext.decode(response.responseText);
        	    	    	if(rs.rst){
        	    	    		Ext.Msg.alert('提示', '删除成功！');
        	    	    		me.zytypePanel.store.reload();
        	    	    	}
        	    	    	else{
        	    	    		Ext.Msg.alert('提示', '删除失败！详情：'+rs.msg);
        	    	    	}
					    }
					});
				}
			});
		}
		else{
			Ext.Msg.alert('提示', '请选择要删除的信息！');
		}
	},
	getForm: function(config,readOnly){
		var me=this;
		return Ext.apply({
			xtype: 'form',
			bodyPadding: 10,
			defaults: {anchor: '100%'},
			items: [{
					name: 'id',
					xtype: 'hidden'
				},{
					name: 'zytypeId',
					id:me.id+"zytypeId",
					xtype: 'hidden'
				},{
					fieldLabel: '作业类型',
					name: 'name',
					id:me.id+"zytypeName",
					xtype: 'textfield',
					maxLength:20,
					readOnly:true
				},{
					fieldLabel: '过程名称',
					name: 'zygcName',
					xtype: 'textfield',
					maxLength:40,
					allowBlank: false
				},{
					xtype: 'textarea',
					fieldLabel: '描述',
					maxLength:100,
					name: 'describe'
			}]
		},config);
	},
	getZyTypeForm: function(config){
		var me=this;
		return Ext.apply({
			xtype: 'form',
			bodyPadding: 10,
			defaults: {anchor: '100%'},
			items: [{
					name: 'id',
					xtype: 'hidden'
				},{
					fieldLabel: '类型名称',
					name: 'name',
					xtype: 'textfield',
					maxLength:50,
					allowBlank: false
				},{
					fieldLabel: '类型编号',
					xtype: 'textfield',
					maxLength:50,
					name: 'code',
					allowBlank: false
			}]
		},config);
	},
	/**
	 * 排序
	 */
	paixu : function(id){
		var me=this;	
		var store=me.bzzyZygcPanel.store;
    	
    	var idArr=[];
        for ( var i = 0; i < store.getCount(); i++) {
        	if(id !=store.getAt(i).data.id){
        		idArr.push(store.getAt(i).data.id);
        	}
		}
         
     	Ext.Ajax.request({
		    url: 'bzzy/BzzyZygcAction/updateJcBzjsbzzySort.do',
		    params: {
		    	idArr:idArr.join(',')
	    	},
	    	success: function(response){
	    		store.reload();
	    	}
		});
	}
});